package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.MemberThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.ReservationService;
import com.zzyl.nursing.vo.TimeCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author: ZHD
 * @Date: 2025/08/23 7:48
 * @Description:
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements ReservationService {


    @Autowired
    private ReservationMapper reservationMapper;
    @Override
    public void addReservation(ReservationDto reservationDto) {
        Reservation reservation = new Reservation();
        BeanUtil.copyProperties(reservationDto, reservation);
        reservation.setStatus(Constants.RESERVATION_STATUS);
        reservation.setCreateTime(LocalDateTime.now());
        reservation.setUpdateTime(LocalDateTime.now());
        LocalDateTime time = reservation.getTime();
        System.out.println(time);
        save(reservation);
    }

    /**
     * 查询用户取消预约数量
     * @param userId 用户id
     * @return 取消预约数量
     */
    @Override
    public Integer getCancelledReservationCount(Long userId) {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        Long count = this.lambdaQuery().eq(Reservation::getStatus, Constants.RESERVATION_STATUS_CANCELLED)
                .between(Reservation::getUpdateTime, startTime, endTime)
                .eq(Reservation::getUpdateBy, userId)
                .count();
        return count.intValue();
    }

    /**
     * 分页查询预约信息
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param status 状态
     * @return 预约信息列表
     */
    @Override
    public TableDataInfo<Reservation> page(Integer pageNum, Integer pageSize, Integer status) {
        Long userId = MemberThreadLocal.getMemberId();
        if (ObjectUtil.isEmpty(userId)) {
            throw new BaseException("请先登录");
        }
        Page<Reservation> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Reservation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(status != null, Reservation::getStatus, status)
                .eq(Reservation::getCreateBy, userId)
                .orderByDesc(Reservation::getCreateTime);
        page(page, wrapper);

        TableDataInfo<Reservation> objectTableDataInfo = new TableDataInfo<>();
        objectTableDataInfo.setTotal(page.getTotal());
        objectTableDataInfo.setRows(page.getRecords());
        objectTableDataInfo.setCode(200);
        objectTableDataInfo.setMsg("查询成功");
        return objectTableDataInfo;
    }

    /**
     * 取消预约
     * @param id 预约id
     */
    @Override
    public void cancelReservation(Long id) {
        Reservation reservation = getById(id);
        if (Objects.isNull(reservation)) {
            throw new RuntimeException("预约不存在");
        }
        reservation.setStatus(Constants.RESERVATION_STATUS_CANCELLED);
        reservation.setUpdateTime(LocalDateTime.now());
        reservation.setUpdateBy(MemberThreadLocal.getMemberId());
        updateById(reservation);
    }

    @Override
    public List<TimeCountVo> countByTime(Long time) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);

        LocalDateTime startTime = localDateTime.toLocalDate().atStartOfDay();

        LocalDateTime endTime = startTime.plusDays(1);
        List<TimeCountVo> timeCountVoList = reservationMapper.countReservationsForTime(startTime, endTime);
        return timeCountVoList;
    }
}
